package org.mentalsmash.tazio.evaluation.tests;

import java.util.Random;

import org.mentalsmash.tazio.evaluation.mock.Counter;
import org.mentalsmash.tazio.transactions.RollbackException;
import org.mentalsmash.tazio.transactions.Share;
import org.mentalsmash.tazio.transactions.management.TransactionStateException;

public class UnsharedCounterTest extends CounterTest {
    
    private final static int sleepTime = 1; //i.e. doesn't sleep;
    
    
    public static void main(String[] args){
	String conf = args[0];
	int numThreads = Integer.parseInt(args[1]);
	System.out.println("bla");
	UnsharedCounterTest instance = new UnsharedCounterTest();
	instance.doJob(conf,numThreads, sleepTime);
    }

    @Override
    public Counter retrieveCounter(Share share) {
	Random rand = new Random();
	String properties ="name:test-counter" + System.currentTimeMillis() + rand.nextInt(1000) ;
	Counter testcounter = share.getObject(properties);
	System.out.println("retrieving " + properties);
	

	if (testcounter == null) {
	    try {
		testcounter = share.createObject(Counter.class, properties);
	    } catch (TransactionStateException e1) {
		e1.printStackTrace();
		throw new RuntimeException("Cannot retrieve counter object!");
	    } catch (RollbackException e1) {
		e1.printStackTrace();
		throw new RuntimeException("Cannot retrieve counter object!");
	    }
	} else {
	    throw new RuntimeException();
	}
	return testcounter;
    }
}
